package com.android.lovingpet.common.userlist;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.bean.Account;

import java.util.ArrayList;
import java.util.List;

public class UserListActivity extends BaseActivity<UserListContract.View, UserListPresenter> implements UserListContract.View {

    public static final String IDS_KEY = "ids";
    public static final String TITLE_KEY = "title";
    public static final String TITLE_FANS = "粉丝";
    public static final String TITLE_ATTENTIONS = "关注";
    public static final String TITLE_BLACKLIST = "黑名单";

    private UserListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);
        ((TextView) findViewById(R.id.tv_title)).setText(getIntent().getStringExtra(TITLE_KEY));
        adapter = new UserListAdapter();
        UserListContract.Presenter presenter = getPresenter();

        ArrayList<Integer> ids = getIntent().getIntegerArrayListExtra(IDS_KEY);
        if (ids == null) {
            ids = new ArrayList<>();
        }
        RecyclerView recyclerView = findViewById(R.id.rv_users);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        presenter.getData(ids);
    }

    @Override
    public UserListPresenter createPresenter() {
        return new UserListPresenter(this);
    }

    @Override
    public UserListContract.View createView() {
        return this;
    }

    @Override
    public void addItem(Account account) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.add(account);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearData() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.clear();
            }
        });
    }
}
